﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class BetPage : EventManager
{
    private static BetPage instance;
    public static BetPage GetInstance()
    {
        return instance;
    }
    void Awake()
    {
        instance = this;
    }

    public override void InformationSetting()
    {
        AddListenerToButton(dissolveBtn, DissolveBtnClick);
        AddListenerToButton(callBtn, CallBtnClick);
    }

    void Start()
    {
    }

    public Button dissolveBtn;
    public Button callBtn;
    public Slider appendSlider;
    public Text appendTex;
    public Text callTex;
    public int betOrCarllNum;

    public GameObject backGround;
    public GameObject betNum;

    public void ShowPageType(int type)                                                                            //1 下注page  2 跟注page
    {
        if (type == 1)
        {
            callBtn.gameObject.SetActive(false);
            appendSlider.gameObject.SetActive(true);
        }
        else if (type == 2)
        {
            callBtn.gameObject.SetActive(true);
            appendSlider.gameObject.SetActive(false);
            callTex.text = betOrCarllNum.ToString();
        }
    }

    private void ShowAppendText(int num)
    {
        appendTex.text = num.ToString();
    }

    private void DissolveBtnClick()
    {
        MessageManager.GetInstance().SendMsg1212(3, GameData.GetInstance().locationIndex, 0);

        CloseBtnClick();
    }

    private void CallBtnClick()
    {
        MessageManager.GetInstance()
            .SendMsg1212(2, GameData.GetInstance().locationIndex, betOrCarllNum);

        CloseBtnClick();
    }

    public void AppendBtnDownEvent()
    {
        if (!backGround.activeInHierarchy)
        {
            backGround.SetActive(true);          
        }

        if (!betNum.activeInHierarchy)
        {
            betNum.SetActive(true);
            ShowAppendText(GameRoomModel.GetInstance().MinScore);
        }

        GameRoomPage.GetInstance().SetAudAndPlay(6);
    }

    public void AppendBtnUpEvent()
    {
        int temp = GameRoomModel.GetInstance().MinScore + (int)Math.Floor(appendSlider.value * (GameRoomModel.GetInstance().MaxLimitScore - GameRoomModel.GetInstance().MinScore));

        MessageManager.GetInstance().SendMsg1212(1, GameData.GetInstance().locationIndex, temp);

        CloseBtnClick();
    }

    public void OnSliderValueChange()
    {
        if (!isPlayAud)
        {
            GameRoomPage.GetInstance().SetAudAndPlay(0);
            isPlayAud = true;
        }    
    }

    private bool isPlayAud;
    private float timer = 0;
    void Update()
    {
        int temp = GameRoomModel.GetInstance().MinScore + (int)Math.Floor(appendSlider.value * (GameRoomModel.GetInstance().MaxLimitScore - GameRoomModel.GetInstance().MinScore));

        ShowAppendText(temp);

        if (isPlayAud)
        {
            timer += Time.deltaTime;
            if (timer > 1)
            {
                isPlayAud = false;
                timer = 0;
            }
        }
    }
}
